<template>
  <el-dialog
    title="添加合同"
    :visible="showDialog"
    width="580px"
    @close="$emit('update:showDialog', false)"
  >
    <!-- 表单区域 -->
    <div class="form-container">
      <el-form ref="addForm" :model="rentForm" :rules="rentRules" label-position="top">
        <el-form-item label="租赁楼宇" prop="buildingId">
          <el-select v-model="rentForm.buildingId" placeholder="请选择">
            <el-option
              v-for="item in buildingList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="租赁起止日期" prop="rentTime">
          <el-date-picker
            v-model="rentForm.rentTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="租赁合同" prop="contractId">
          <el-upload
            action="#"
            :http-request="upload"
          >
            <el-button size="small" type="primary" plain>上传合同文件</el-button>
            <div slot="tip" class="el-upload__tip">支持扩展名：.doc .docx .pdf, 文件大小不超过5M</div>
          </el-upload>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button size="mini">取 消</el-button>
      <el-button size="mini" type="primary" @click="confirm">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getBuilding, upload, addContract } from '@/api/Enterprise'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    value: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      rentForm: {
        buildingId: null, // 楼宇id
        contractId: null, // 合同id
        contractUrl: '', // 合同Url
        enterpriseId: null, // 企业名称
        type: 0, // 合同类型
        rentTime: [] // 合同时间
      },
      buildingList: [],
      rentRules: {
        buildingId: [
          { required: true, message: '请选择楼宇', trigger: 'change' }
        ],
        rentTime: [
          { required: true, message: '请选择租赁日期', trigger: 'change' }
        ],
        contractId: [
          { required: true, message: '请上传合同文件' }
        ]
      }
    }
  },
  async created() {
    const res = await getBuilding()
    // console.log(res)
    this.buildingList = res.data
  },
  methods: {
    // 上传合同
    async upload(data) {
      const file = data.file
      // 处理formData类型参数
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 'contract')
      // 上传文件、解构赋值
      const res = await upload(formData)
      console.log(res)
      const { id, url } = res.data
      this.rentForm.contractId = id
      this.rentForm.contractUrl = url
      // 上传完毕单独校验
      this.$refs.addForm.validateField('contractId')
    },
    async confirm() {
      await this.$refs.addForm.validate()
      this.rentForm.enterpriseId = this.value
      // console.log(this.rentForm)
      const { buildingId, contractUrl, contractId, type, enterpriseId } = this.rentForm
      await addContract({ buildingId, contractUrl, contractId, type, enterpriseId, startTime: this.rentForm.rentTime[0], endTime: this.rentForm.rentTime[1] })
      this.$message.success('添加合同成功')
      this.$emit('update:showDialog', false)
      this.$emit('update')
    }
  }
}
</script>
